package models;

import com.google.gdata.data.media.mediarss.MediaThumbnail;
import com.google.gdata.data.photos.PhotoEntry;
import siena.*;
import siena.embed.Embedded;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Cyril
 * Date: 07.08.11
 * Time: 16:39
 * To change this template use File | Settings | File Templates.
 */
@Table("picasa_photo")
public class PicasaPhoto {

    @Id(Generator.NONE)
    public Long id;

    @Column("version")
    @Max(50)
    public String version;

    @Column("album")
    @Index("album_index")
    public PicasaAlbum album;

    @Column("photo")
    public Photo photo;

    @Embedded
    public List<String> thumb;

    @Override
    public String toString() {
        return id != null ? Long.toString( id ) : null;
    }

    public static Query<PicasaPhoto> all() {
        return Model.all(PicasaPhoto.class);
    }

    public static PicasaPhoto getFromEntry(PhotoEntry entry) {
        PicasaPhoto photo = new PicasaPhoto();
        photo.id = Long.valueOf( parseId( entry.getId() ) );
        photo.version = entry.getUpdated().toString();
        photo.thumb = new ArrayList<String>();
        for (MediaThumbnail thumb : entry.getMediaThumbnails())
            photo.thumb.add( thumb.getUrl() );
        photo.thumb.add( entry.getMediaContents().get(0).getUrl() );
        return photo;
    }

    public static String parseId(String resource) {
        return resource.substring( resource.lastIndexOf("/") + 1 );
    }

}
